AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')
local Ground = 1 + 0 + 2 + 8 + 32
local rel = "vehicles/tank_readyfire1.wav"

function ENT:Initialize()
	self.orginalposition = self.Entity:GetPos()
	self.Entity:SetModel(  "models/descent/descent3pyroship.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType(  MOVETYPE_VPHYSICS   )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	local pos = self.Entity:GetPos()
	for var=1, 20, 1 do 
		local zonetype = "Sector" .. var
		if	CheckPoint(pos.X,pos.Y,pos.Z,zonetype) then
			self.currentsector = var
		end
	end

	self.Entity:SetName("Minigun Defence Tower")
	self.hp = 20000
	self.Physik = self.Entity:GetPhysicsObject()
	self.Entity:StartMotionController()
	self.active = true
end


function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 36
	local ent = ents.Create( "npc_Enemy1" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Think()
	self.Entity:NextThink( CurTime() + 2 )
	for index,ply in pairs(player.GetAll( )) do	
		local plyuni = ply:UniqueID( )
		if (servertable["ply"][plyuni]["currentsector"] == self.currentsector) then
			for k,core in pairs(servertable["ship"]["shipcores"]) do 
				if IsOwner(core,ply) then
					npctarget(core)
					self.Entity:NextThink( CurTime() + 0.25 )
					local pos = self.Entity:GetPos()
					local corepos = core:GetPos()
					local distance = pos:Distance( corepos )	
					local move = Vector(0,0,0)
					if (distance < 1000) then
						move = move + self.Entity:GetRight( )*50 + self.Entity:GetUp( )*50
					else
						move = move + self.Entity:GetForward( )*100
					end
					local aimangle = (corepos - pos):Normalize()
					self.Angles = aimangle:Angle( )
					self.pos = pos + move
					local Startpos =  self.Entity:GetPos() + (aimangle * 50) 
					bullet = {} 
					bullet.Src			= Startpos
					bullet.Dir			= aimangle
					bullet.Spread		= Vector(0.02,0.02,0.02) 
					bullet.Num			= 1 
					bullet.Damage		= 80 
					bullet.Force		= 1000 
					bullet.Tracer		= 1
					bullet.TracerName	= "Tracer"
					self.Entity:FireBullets( bullet )
					self.Physik:Wake()
				end
			end
		end
	end	
	return true
end

function ENT:PhysicsSimulate() 
	self.ShadowParams={} 
	self.ShadowParams.secondstoarrive	= 2
	self.ShadowParams.pos			= self.pos
	self.ShadowParams.angle			= self.Angles
	self.ShadowParams.maxangular		= 15
	self.ShadowParams.maxangulardamp	= 1000
	self.ShadowParams.maxspeed		= 100
	self.ShadowParams.maxspeeddamp		= 1000
	self.ShadowParams.dampfactor		= 0.7
	self.ShadowParams.teleportdistance	= 0
	self.ShadowParams.deltatime		= 10
	self.Physik:ComputeShadowControl(self.ShadowParams) 
 end  

function ENT:OnTakeDamage(info)
	local effectdata = EffectData()
	effectdata:SetStart( self.Entity:GetPos() )
	effectdata:SetOrigin( self.Entity:GetPos() )
	effectdata:SetScale( 30 )
	effectdata:SetMagnitude( 40 )
	effectdata:SetEntity(self.Entity)
	util.Effect( "shield_hit", effectdata )
	self.hp = self.hp  - info:GetDamage()

	if (self.hp < 1) then
		if not self.exploded then
			timer.Create("Respawn" .. tostring(self.orginalposition),60,1,respawnnpc,self.Entity:GetClass(),self.orginalposition)
			local 	Buyedentity = ents.Create("moneydrop")
			Buyedentity:SetAngles(Vector(0,0,0))
			Buyedentity:SetPos(self.Entity:GetPos())
			Buyedentity:Spawn()
			Buyedentity.amount = 200
			Buyedentity:GetPhysicsObject():EnableGravity( false ) 
			Buyedentity:GetPhysicsObject():EnableDrag( false )	
			if ( Buyedentity:GetPhysicsObject():IsValid() ) then
				Buyedentity:Activate()	
			end
			local effectdata = EffectData()
			effectdata:SetStart( self.Entity:GetPos() )
			effectdata:SetOrigin( self.Entity:GetPos() )
			effectdata:SetScale( 300 )
			effectdata:SetMagnitude( 400 )
			effectdata:SetEntity(self.Entity)
			util.Effect( "Plasme_exp_small", effectdata )
			self.Entity:Remove()
			self.exploded = true
		end
	end
end
